<template>
  <div>
    <el-card>
      <el-row>
        <el-col>
          <el-button class="addBtn" type="primary" @click="addUserRoles">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 权限表单 -->
      <el-row>
        <el-table
          border
          stripe
          ref="rightTable"
          :data="allRoleList"
          style="width: 100%"
        >
          <el-table-column type="expand">
            <template v-slot="scope">
              <el-row
                :class="['bdBottom', 'rowCenter', index1 === 0 ? 'bdtop' : '']"
                v-for="(item, index1) in scope.row.children"
                :key="item.id"
              >
                <!-- 一级权限 -->
                <el-col :span="5">
                  <el-tag closable @close="removeTagById(scope.row, item.id)">
                    {{ item.authName }}
                  </el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="19">
                  <el-row
                    :class="[index2 === 0 ? '' : 'bdtop', 'rowCenter']"
                    v-for="(item2, index2) in item.children"
                    :key="item2.id"
                  >
                    <!-- 二级权限 -->
                    <el-col :span="6">
                      <el-tag
                        closable
                        type="success"
                        @close="removeTagById(scope.row, item2.id)"
                      >
                        {{ item2.authName }}
                      </el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <!-- 三级权限 -->
                    <el-col :span="18">
                      <el-tag
                        @close="removeTagById(scope.row, item3.id)"
                        closable
                        type="waring"
                        v-for="item3 in item2.children"
                        :key="item3.id"
                        >{{ item3.authName }}</el-tag
                      >
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index" label="索引"></el-table-column>
          <el-table-column
            property="roleName"
            label="角色名称"
          ></el-table-column>
          <el-table-column
            property="roleDesc"
            label="角色描述"
          ></el-table-column>
          <el-table-column label="操作" min-width="260px">
            <template v-slot="scope">
              <el-button
                @click="changeUserRoles(scope.row.id)"
                type="primary"
                icon="el-icon-edit"
                size="small"
                >修改</el-button
              >
              <el-button
                @click="delRoleInfo(scope.row.id)"
                type="danger"
                icon="el-icon-delete"
                size="small"
                >删除</el-button
              >

              <el-button type="warning" icon="el-icon-setting" size="small" @click="changeUserRight(scope.row)"
                >分配权限</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>
    </el-card>
    <change-user-right-box @changeRoleInfoDrive='getAllUserRights' ref="changeUserRightBox"></change-user-right-box>
    <addUserRolesBox @getRoleInfoDrive='getAllUserRights'  ref="addUserRolesBox"></addUserRolesBox>
  </div>
</template>

<script>
import { getAllRoleInfoApi, delUserRollApi, delRoleInfoApi } from '@/Utils/Api/getUserRightApi'
import changeUserRightBox from './changeUserRightBox.vue'
import addUserRolesBox from './addUserRolesBox.vue'
export default {
  name: 'RolesView',
  data () {
    return {
      allRoleList: []
    }
  },
  components: {
    changeUserRightBox,
    addUserRolesBox
  },
  created () {
    this.getAllUserRights()
  },
  mounted () {},
  methods: {
    async getAllUserRights () {
      const { data } = await getAllRoleInfoApi()
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: data.meta.msg
        })
      }
      this.allRoleList = data.data
      console.log(data)
      this.$message({
        type: 'success',
        message: data.meta.msg
      })
    },
    async removeTagById (role, rightId) {
      const confirmRES = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)
      if (confirmRES !== 'confirm') {
        return this.$message({
          type: 'error',
          message: '用户取消了删除'
        })
      }
      const { data } = await delUserRollApi(role.id, rightId)
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: '删除失败'
        })
      }
      this.$message({
        type: 'success',
        message: '用户删除了该权限'
      })
      role.children = data.data
    },
    changeUserRight (row) {
      this.$nextTick(() => {
        this.$refs.changeUserRightBox.dialogVisible = true
        this.$refs.changeUserRightBox.role = row
      })
    },
    // 添加用户
    addUserRoles () {
      this.$nextTick(() => {
        this.$refs.addUserRolesBox.dialogVisible = true
        this.$refs.addUserRolesBox.viewStatus = true
      })
    },
    // 修改用户
    changeUserRoles (id) {
      this.$nextTick(() => {
        this.$refs.addUserRolesBox.dialogVisible = true
        this.$refs.addUserRolesBox.viewStatus = false
        this.$refs.addUserRolesBox.roleId = id
      })
    },
    // 删除角色
    async  delRoleInfo (id) {
      const confirmStatus = await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmStatus !== 'confirm') {
        return this.$message({
          type: 'error',
          message: '用户取消了删除'
        })
      }
      const { data } = await delRoleInfoApi(id)
      this.getAllUserRights()
      this.$message({
        type: 'success',
        message: data.meta.msg
      })
    }
  },
  computed: {},
  watch: {}
}
</script>

<style lang="less" scoped>
.addBtn {
  margin-bottom: 12px;
}
.el-tag {
  margin: 10px 5px 10px 10px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdBottom {
  border-bottom: 1px solid #eee;
}
.rowCenter {
  display: flex;
  align-items: center;
}
</style>
